package com.chen.pfm;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class BaseInfoDataAbility extends Ability {
     static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x2022, "BaseInfoDataAbility");
    private  String tableName="baseinfo";
    //RdbStore 对象就表示与数据库的连接，通过此对象的连接完成数据表的curd
    private RdbStore rdbStore;
    //StoreConfig配置数据库文件
    StoreConfig config = StoreConfig.newDefaultConfig("pfms.db");
    //成功回调
    RdbOpenCallback rdbOpenCallback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            HiLog.info(LABEL, "创建数据表"+tableName);
            //创建数据表
            String sql="create table if not exists "+tableName +
                    "(id integer primary key autoincrement," +
                    "key  text not null unique," +
                    "value text not null)" ;
            rdbStore.executeSql(sql);
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {

        }
    };
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        //初始化数据库连接，创建数据库，成功执行回调
        DatabaseHelper databaseHelper = new DatabaseHelper(this);
        rdbStore = databaseHelper.getRdbStore(config,1,rdbOpenCallback);//ctrl + p 查看函数接口
        HiLog.info(LABEL, "数据表"+tableName  +" ===>> 初始化成功！");
    }

    /**
     *
     * @param uri
     * @param columns 需要查询的列
     * @param predicates  封装查询的条件，需要针对那一张表的条件封装
     * @return
     */
    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        RdbPredicates users = DataAbilityUtils.createRdbPredicates(predicates, tableName);
        ResultSet resultSet = rdbStore.query(users, columns);
        if(resultSet!=null){
            HiLog.error(LABEL, "数据表"+tableName  +"查询失败");
        }else{
            HiLog.info(LABEL, "数据表"+tableName  +" ===>> 进行了查询操作！");
        }
        return resultSet;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        String path=uri.getLastPath();
        int i=-1;
        i= (int) rdbStore.insert(path,value);
        if(i<0){
            HiLog.error(LABEL, "数据表"+tableName  +"插入失败");
        }else{
            HiLog.info(LABEL, "数据表"+tableName  +" ===>> 进行了插入操作！");
        }


        return i;
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, tableName);
        int i=rdbStore.delete(rdbPredicates);
        if(i<0){
            HiLog.error(LABEL, "数据表"+tableName  +"删除失败");
        }else{
            HiLog.info(LABEL, "数据表"+tableName  +" ===>> 进行了删除操作！");
        }
        return i;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        String table=uri.getLastPath();
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, tableName);
        int i=rdbStore.update(value,rdbPredicates);
        HiLog.info(LABEL, "数据表"+tableName  +" ===>> 进行了更新操作！");
        return i;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}